﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace WMS_DAL
{
    public class BaseDAL<T> : SimpleClient<T> where T : class, new()
    {
        public BaseDAL(ISqlSugarClient db)
        {
            base.Context = db;
        }
        public async Task<bool> CreateAsync(T entity)
        {
            return await base.InsertAsync(entity);
        }
        public async Task<bool> EditAsync(T entity)
        {
            return await base.UpdateAsync(entity);
        }
        public async Task<T> FindAsync(int id)
        {
            return await base.GetByIdAsync(id);
        }
        public async Task<int> FastInsertRangeAsync(List<T> list)
        {
            return await base.Context.Fastest<T>().BulkCopyAsync(list);
        }
        //大数据批量更新  适合列多数据多的更新
        public async Task<int> BulkUpdateAsync(List<T> list)
        {
            return await base.Context.Fastest<T>().BulkUpdateAsync(list);
        }
        public async Task<List<T>> QueryListAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await Context.Queryable<T>().Where(whereLambda).ToListAsync();
        }
      

        public async Task<bool> DeleteByIdAsync(int id)
        {
            return await base.DeleteByIdAsync(id);
        }
        public async Task<int> DeleteRangeAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await base.Context.Deleteable(whereLambda).ExecuteCommandAsync();
        }
        //查询条件记录后锁表
        public async Task<List<T>> BlockTranAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await base.Context.Queryable<T>().TranLock(DbLockType.Wait).Where(whereLambda).ToListAsync();//返回条数尽量最少尽量
        }
        public async Task<bool> QueryListIsExistAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await Context.Queryable<T>().Where(whereLambda).AnyAsync();
        }
        public async Task<List<T>> ToPageListAsync(int PageNow, int PageSize)
        {
            return await base.Context.Queryable<T>().Skip(PageSize * (PageNow - 1)).Take(PageSize).ToListAsync();
        }
        public async Task<List<T>> ToWherePageListAsync(int PageNow, int PageSize, Expression<Func<T, bool>> whereLambda)
        {
            return await base.Context.Queryable<T>().Where(whereLambda).Skip(PageSize * (PageNow - 1)).Take(PageSize).ToListAsync();
        }

       
    }
}
